package com.jackie.tongzhuang;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jackie.tongzhuang.Classes.BaseClass;
import com.jackie.tongzhuang.Controls.TitleBar;
import com.jackie.tongzhuang.DataBase.DbClass;
import com.jackie.tongzhuang.Enums.Constants;
import com.jackie.tongzhuang.Models.FenQi;

public class FenQiXiangQingActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView ghstext,jhrqtext,jhjetext,jhlbtext;
    private TextView qstext,jetext,bjtext,lxtext,fqrqtext,chrqtext;
    private ImageView ychimg,ydzimg;
    private Button tqhkbut;
    private TitleBar titleBar;
    private DbClass dbClass = new DbClass(this);
    private int fqid = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fenqi_xiangqing);
        initView();

        Intent intent = getIntent();
        fqid = intent.getIntExtra("id",0);

        titleBar.setOnClickedListener(new TitleBar.TitleBarClickListener() {
            @Override
            public void backClick(View v) {
                finish();
            }

            @Override
            public void submitClick(View v) {

            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.v("msgg","fqid="+fqid);
        FenQi fq = dbClass.getFenQiById(fqid);
        initData(fq);
    }

    private void initView(){
        ghstext = findViewById(R.id.fqxq_ghs);
        jhrqtext = findViewById(R.id.fqxq_jhrq);
        jhjetext = findViewById(R.id.fqxq_jhje);
        jhlbtext = findViewById(R.id.fqxq_lb);
        qstext = findViewById(R.id.fqxq_qs);
        jetext = findViewById(R.id.fqxq_je);
        bjtext = findViewById(R.id.fqxq_bj);
        lxtext = findViewById(R.id.fqxq_lx);
        fqrqtext = findViewById(R.id.fqxq_fqrq);
        chrqtext = findViewById(R.id.fqxq_chrq);
        ychimg = findViewById(R.id.fqxq_ych);
        ydzimg = findViewById(R.id.fqxq_ydz);
        tqhkbut = findViewById(R.id.fqxq_tqhk);
        titleBar = findViewById(R.id.fqxq_titlebar);
        tqhkbut.setOnClickListener(this);
    }
    private void initData(FenQi fq){
        boolean isych = (fq.getYiChangHuan() == 0 ? false : true);
        if(isych){
            ychimg.setVisibility(View.VISIBLE);
            ydzimg.setImageResource(R.mipmap.yidaozhang);
        }else{
            ychimg.setVisibility(View.GONE);
            ydzimg.setImageResource(R.mipmap.daishoukuan);
        }
        ghstext.setText(fq.getJinHuoDan().getGongHuoShang().getMingCheng());
        jhrqtext.setText(fq.getJinHuoDan().getJinHuoRiQi());
        jhjetext.setText("¥ "+ BaseClass.getDecimalToStr(2,fq.getJinHuoDan().getJinHuoJinE()));
        jhlbtext.setText(Constants.leiBie[fq.getJinHuoDan().getLeiBieId()]);
        qstext.setText("第 "+fq.getQiShu()+" 期");
        jetext.setText("¥ "+ BaseClass.getDecimalToStr(2,fq.getFenQiJinE()));
        bjtext.setText("¥ "+ BaseClass.getDecimalToStr(2,fq.getBenJin()));
        lxtext.setText("¥ "+ BaseClass.getDecimalToStr(2,fq.getLiXi()));
        fqrqtext.setText(fq.getFenQiRiQi());
        chrqtext.setText(fq.getChangHuanRiQi());
        if(isych){
            tqhkbut.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.fqxq_tqhk:
                //提前还款
                AlertDialog dialog = new AlertDialog.Builder(this).create();
                dialog.setTitle("提前还款提醒");
                dialog.setMessage("确定现在提前还款吗？");
                dialog.setButton(DialogInterface.BUTTON_POSITIVE,"确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        ContentValues values = new ContentValues();
                        values.put("YiChangHuan",1);
                        values.put("ChangHuanJinE",jetext.getText().toString().split("¥")[1]);
                        dbClass.update("FenQi",values,fqid);
                        dialog.dismiss();
                        finish();
                    }
                });
                dialog.setButton(DialogInterface.BUTTON_NEGATIVE,"取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialog.dismiss();
                    }
                });
                dialog.show();

                break;
        }
    }
}